package com.hsource.common.utils;

import lombok.Getter;
import lombok.Setter;
import org.springframework.validation.BindingResult;

/**
 * 通用返回对象
 * @Author itoug
 * @Crreate 2020-09-04 8:57
 */
@Getter
@Setter
public class CommonResp {

    //操作成功
    public static final int SUCCESS = 0;
    //操作失败
    public static final int FAILED = -1;
    //支付失败
    public static final int PAY_FAILED = 1;
    //参数校验失败
    public static final int VALIDATE_FAILED = 404;
    //未认证
    public static final int UNAUTHORIZED = 401;
    //未授权
    public static final int FORBIDDEN = 403;
    private int code;
    private String message;
    private Boolean success;
    private CommonAuth auth;
    private Object data;

    /**
     * 普通成功返回
     *
     * @param data 获取的数据
     */
    public CommonResp success(Object data) {
        this.code = SUCCESS;
        this.message = "操作成功";
        this.success=true;
        this.data = data;
        return this;
    }
    /**
     * 普通成功返回
     *
     */
    public CommonResp success() {
        this.code = SUCCESS;
        this.message = "操作成功";
        this.data =  "操作成功";
        this.success=true;
        return this;
    }
    /**
     * 普通成功返回
     */
    public CommonResp success(String msg, Object data) {
        this.code = SUCCESS;
        this.message = msg;
        this.success=true;
        this.data = data;
        return this;
    }


    /**
     * 普通失败提示信息
     */
    public CommonResp failed() {
        this.code = FAILED;
        this.message = "操作失败";
        this.success=false;
        return this;
    }

    public CommonResp failed(Integer code) {
        this.code = code;
        this.message = "操作失败";
        this.success=false;
        return this;
    }

    public CommonResp failed(String msg) {
        this.code = FAILED;
        this.message = msg;
        this.success=false;
        return this;
    }

    /**
     * 参数验证失败使用
     *
     * @param msg 错误信息
     */
    public CommonResp validateFailed(String msg) {
        this.code = VALIDATE_FAILED;
        this.message = msg;
        this.success=false;
        return this;
    }

    /**
     * 未登录时使用
     *
     * @param msg 错误信息
     */
    public CommonResp unauthorized(String msg) {
        this.code = UNAUTHORIZED;
        this.message = "暂未登录或token已经过期";
        this.data = msg;
        this.success=false;
        return this;
    }

    /**
     * 未授权时使用
     *
     * @param msg 错误信息
     */
    public CommonResp forbidden(String msg) {
        this.code = FORBIDDEN;
        this.message = "没有相关权限";
        this.data = msg;
        this.success=false;
        return this;
    }

    /**
     * 参数验证失败使用
     *
     * @param result 错误信息
     */
    public CommonResp validateFailed(BindingResult result) {
        validateFailed(result.getFieldError().getDefaultMessage());
        return this;
    }
    /**
     * 普通失败提示信息
     */
    public CommonResp paramFailed() {
        this.code = FAILED;
        this.message = "参数失败";
        this.success=false;
        return this;
    }

    public CommonResp paramFailed(String msg) {
        this.code = FAILED;
        this.message = msg;
        this.success=false;
        return this;
    }
}
